<template> 
  <!--主题管理  自营商城主题 h5主题 -->
  <div class="app-container">
    <el-form class="search-form-new" :model="queryParams" ref="queryForm" :inline="true" label-width="auto">
      <!-- <el-form-item label="名称" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入名称" clearable/>
      </el-form-item>
      <el-form-item label="渠道类型" prop="channelType">
        <el-select clearable v-model="queryParams.channelType" placeholder="请选择">
          <el-option label="接口" :value="1"></el-option>
          <el-option label="脚本" :value="2"></el-option>
        </el-select>
      </el-form-item> -->
      <el-form-item class="btns"> 
        <!-- <el-button type="primary" icon="el-icon-search" @click="handleQuery">搜索</el-button> -->
        <el-button v-hasPermi="['themeManage:add']" type="success"  icon="el-icon-plus" @click="handleAdd">新增</el-button>
      </el-form-item>
    </el-form>
    <div class="table_box">
      <el-table stripe v-loading="loading" :data="accountList" >
        <!-- <el-table-column type="selection" width="55" /> -->
        <el-table-column label="主题ID" prop="id" width="100"/>
        <el-table-column label="主题名称" prop="name" />
        <el-table-column label="主题唯一标识" prop="key" ></el-table-column>
        <!-- <el-table-column label="客服联系方式" prop="serviceContact" min-width="150"> </el-table-column>
        <el-table-column label="客服姓名" prop="serviceName" min-width="150"> </el-table-column> -->
        <el-table-column label="备注" prop="remark" />
        <el-table-column label="排序" prop="sort" > </el-table-column>
        <el-table-column label="是否展示" prop="isShow">
          <template slot-scope="scope">
            <div>
                {{commonMaps.isYesMap[scope.row.isShow]}}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope">
            <el-button v-hasPermi="['channelList:edit']" type="edit" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
            <!-- <el-button type="text" icon="el-icon-edit" @click="addGame(scope.row)">关联游戏</el-button> -->
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
        @pagination="getList" />
    </div>

    <!-- 添加或修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
      <el-form ref="form" :inline="true" :model="form" :rules="rules" label-width="auto">
        <el-form-item label="主题名称" prop="name">
          <el-input v-model="form.name" style="width:200px" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="主题唯一标识" prop="key">
          <el-input style="width:200px" v-model="form.key" placeholder="请输入主题唯一标识" />
        </el-form-item><br/>
        <el-form-item label="图片" prop="img">
          <div class="flex" :class="{ 'hiddenupload': qrCodeList.length }">
            <el-input v-model="form.img" v-show="false"></el-input>
            <el-upload class :action="uploadImgUrl" :file-list="qrCodeList" :show-file-list="true"
              list-type="picture-card" :limit="1" :on-success="handleAvatarSuccess" :on-remove="handleAvatarRemove"
              :on-preview="handlePictureCardPreview" accept="image/png, image/jpeg">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </el-form-item><br/>
        <!-- <el-form-item label="客服姓名" prop="serviceName">
          <el-input style="width:200px" v-model="form.serviceName" placeholder="请输入客服姓名" />
        </el-form-item>
        <el-form-item label="客服联系方式" prop="serviceContact">
          <el-input style="width:200px" v-model="form.serviceContact" placeholder="请输入客服联系方式" />
        </el-form-item><br/> -->
        <el-form-item label="是否展示">
          <el-radio-group v-model="form.isShow">
            <el-radio :label="1">是</el-radio>
            <el-radio :label="2">否</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input-number  v-model="form.sort" placeholder="请输入" />
        </el-form-item><br/>
        <el-form-item label="备注" prop="remark">
          <el-input style="width:600px" type="textarea" rows="5" v-model="form.remark" placeholder="请输入" />
        </el-form-item><br/>
        <el-form-item prop="buyWay">
          <span slot="label">
            <el-tooltip content="控制该主题开通时一次性永久开通，还是按时长开通，修改该参数不影响已开通的用户。" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            计费方式
          </span>
          <el-radio-group v-model="form.buyWay">
            <el-radio
              v-for="(item,key) in commonMaps.buyWayMap"
              :key="key"
              :label="key"
            >{{item.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item prop="monthAmount" label="">
          <span slot="label">
            <el-tooltip content="开通该功能多少虞豆，计费方式为月租时填写一个月的费用" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            开通虞豆
          </span>
          <el-input type="number" style="width:120px" v-model="form.monthAmount" placeholder="" />
        </el-form-item>
        <el-form-item prop="minMonthDuration" v-if="form.buyWay==2">
          <span slot="label">
            <el-tooltip content="控制该分组开通时最低开通月数。" placement="top">
              <i class="el-icon-question"></i>
            </el-tooltip>
            起开时长
          </span>
          <el-input-number style="width:150px" v-model="form.minMonthDuration" placeholder="" />月
        </el-form-item><br/>
       
        <div class="dialog-footer">
          <el-button @click="cancel">取 消</el-button>
          <el-button type="primary" @click="submitForm">确 定</el-button>
        </div>
      </el-form>
      
    </el-dialog>
  </div>
</template>

<script>
import {  delAccount, exportAccount } from "@/api/info/check";
import {  addTheme, getThemeList,editTheme } from "@/api/system/themeManage";

import supplyConfigApi from "@/api/system/supplyConfig"
export default {
  name: "",
  components:{},
  dicts: [],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 账号校验表格数据
      accountList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        name: null,
        isShow:'',
        isPage:2
      },
      qrCodeList:[],
      uploadImgUrl:'',
      // 表单参数
      form: {
        name:'',
        key:'',
        isShow:1,
        sort:'',
        remark:'',
        buyWay:'',
        monthAmount:'',
        minMonthDuration:'',
      },
   
      allUsers:[],
      // 表单校验
      rules: {
        name: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        key: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        serviceName: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        serviceContact: [
          { required: true, message: "请输入", trigger: "blur" }
        ],
        buyWay: [
          { required: true, message: "计费方式不能为空", trigger: "change" }
        ],
        monthAmount: [
          { required: true, message: "费用不能为空", trigger: "change" }
        ],
        minMonthDuration: [
          { required: true, message: "最低开通时长不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.uploadImgUrl = process.env.VUE_APP_BASE_API + "/upload/image";
    this.getList();
  },
  methods: {
  // // 获取所有用户
  getAllUserList(val) {
      supplyConfigApi.getAllUserList().then(res => {
        this.allUsers = res.rows
      })
    },
   
    getList() {
      this.loading = true;
      getThemeList(this.queryParams).then(response => {
        this.accountList = response.rows;
        // this.total = response.total;
        this.loading = false;
      });
    },
   
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
  
    // 选择用户改变
    userChange(e){
      // console.log("%c Line:217 🍯 e", "color:#4fff4B", e);
      this.allUsers.forEach(r => {
        if(r.userId==e){
          this.form.userName = r.nickName
          this.form.contact = r.phonenumber
        }
      });

    },
    // 表单重置
    reset() {
      this.form = {
        name:'',
        key:'',
        isShow:1,
        sort:'',
        remark:'',
        buyWay:'',
        monthAmount:'',
        minMonthDuration:'',
      };
      this.selectGameIds = []
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      // this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        // pageNum: 1,
        // pageSize: 10,
        name: null,
        isPage:2
      }
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.qrCodeList=[]
      this.form.img = ''
      this.open = true;
      this.title = "新增主题";
      this.$set(this.form,'buyWay','2')
      this.form.minMonthDuration = 1
      // this.getUserList()
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row))
      if(this.form.buyWay) this.form.buyWay= String(this.form.buyWay);
      if(this.form.img){
        this.qrCodeList = [{
            url:this.form.img,
            name:''
          }]
      }
      this.open = true;
      this.title = "修改主题";
    },
    // 图片预览
    handlePictureCardPreview(file) {
      this.$viewerApi({
        images: [file.url]
      });
    },
    // 标题图片移除
    handleAvatarRemove(response, file) {
      this.form.img = "";
      this.qrCodeList = [];
    },
    // 标题图片上传成功
    handleAvatarSuccess(res, file) {
      if (res.code == 200) {
        let url = res.data.filePath;
        let name = res.data.fileName;
        let param = {};
        param.url = url;
        param.name = name;
        this.qrCodeList.push(param);
        this.form.img = res.data.filePath;
      }
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
           editTheme(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addTheme(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除id为"' + ids + '"的数据项？').then(function () {
        return delAccount(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
  }
};
</script>
<style scoped>

</style>